<?php

require_once BaseUtil::getModulePath("base")  .  '/controllers/BaseController.php';
require_once BaseUtil::getModulePath("acl") . '/services/AclService.php';
require_once BaseUtil::getModulePath('acl') . '/forms/RoleForm.php';
require_once BaseUtil::getModulePath('acl') . '/forms/UserForm.php';
require_once BaseUtil::getModulePath('acl') . '/constants/Permission.php';
require_once BaseUtil::getModulePath('dbgrid') . '/DBGrid.php';
class Acl_AclController extends BaseController
{

    public function init(){
        parent::init();
	}
    
    public function indexAction ()
    {
        $this->_redirect('/acl/acl/getrole');
    }
    
    public function getroleAction ()
    {
    	$dbgrid = new DBGrid($this->view);
        $dbgrid->setPost($_POST);
        $dbgrid->setFilterAction('getrole');
        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "roles-role_name", $this->_translate->translate("name"));
        $dbgrid->addFetchTable("roles", array('id' , 'role_name' , 'created'));
        $dbgrid->addValueFilter('roles-is_delete', '0');
        $dbgrid->addFetchColumn("role_name", $this->_translate->translate("roleName") , "editrole");
        $dbgrid->addFetchColumn("created", $this->_translate->translate("createDate"));
        $dbgrid->setModule('/acl/acl');
        if(AAService::isAllowed("acl", "editrole")){
            $dbgrid->addModuleAction("edit",$this->_translate->translate("editBtn"),"editrole");
        }
        if(AAService::isAllowed("acl", "deleterole")){
            $dbgrid->addModuleAction("delete",$this->_translate->translate("delBtn"),"deleterole");
        }
        if(AAService::isAllowed("acl", "addrole")){
            $dbgrid->addModuleAction("add",$this->_translate->translate("addBtn"),"addrole");
        }
        $dbgrid->setPageTitle($this->_translate->translate("searchTitle"));
        $this->view->dbGrid = $dbgrid->render();
    }
    public function editroleAction ()
    {
        //get all permissions
        $permissions = Permission::getAllPermissions();
        
        $role_id = $this->_getParam('id');
        $aclserv = new AclService();
        $role = $aclserv->getRoleById($role_id);
        $rolePermissions = $aclserv->getPermissionsByRoleId($role_id);
        $form = new RoleForm($permissions,$role,$rolePermissions);
        $this->view->form = $form;
        $this->view->id=$role_id; 
    }
    public function editrolesubmitAction ()
    {
		$name = HttpUtil :: GetPostString('name');
		$role_id = HttpUtil :: GetPostString('role_id');
        //get all permissions
        $permissions = Permission::getAllPermissions();
        $aclserv = new AclService();
        $role = $aclserv->getRoleById($role_id);
        $permission_ids = HttpUtil::getPostValue("rolePermission");
        
        $form = new RoleForm($permissions,$role,$permission_ids);
        if (! $form->isValid($_POST)) {
            $this->view->form = $form;
            return $this->render('editrole');
        }
        
        $role = array ('role_name'=> $name);
        $aclserv->updateRole($role, $role_id,$permission_ids);
        $this->_redirect('/acl/acl/getrole');
    }
    public function addroleAction ()
    {
        //get all permissions
        $permissions = Permission::getAllPermissions();
        
        $form = new RoleForm($permissions);
        $form->setAction('addrolesubmit');
        $this->view->form = $form;
    }
    public function addrolesubmitAction ()
    {
        //get all permissions
        $permissions = Permission::getAllPermissions();

        $form = new RoleForm($permissions);
        if (! $form->isValid($_POST)) {
            $this->view->form = $form;
            return $this->render('addrole');
        }
        $roleName = HttpUtil::getPostString('name');
        $permission_ids = HttpUtil::getPostValue("rolePermission");
        $aclserv = new AclService();
        $role = array( 'role_name' => $roleName);
        $aclserv->addRole($role,$permission_ids);
        $this->_redirect('/acl/acl/getrole');
    }
    public function deleteroleAction ()
    {
        $idsStr = HttpUtil::getPostValue("selectIds");
        $ids = explode(",",$idsStr);
		$aclserv = new AclService();
		$aclserv->deleteAllRoles($ids);
		$this->_redirector->gotoUrl('acl/acl');
    }
    public function deleteAction() {
    	$id=$this->_getParam('id');
    	$aclserv=new AclService();
    	$aclserv->delete($id);
    	$this->_redirector->gotoUrl('acl/acl');
    }
//    public function getuserAction ()
//    {
//        $dbgrid = new DBGrid($this->view);
//        $dbgrid->setPost($_POST);
//        $dbgrid->setFilterAction('getuser');
//        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "admins-username", "UserName");
//        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_TEXT, "admins-email", "Email");
//        $dbgrid->addFilter(DBGridConstant::FILTER_TYPE_DATE, "admins-last_login", "LastLogin");
//        $group[''] = "-select-";
//        $group[0] = "active";
//        $group[1] = "block";
//        $dbgrid->addOptionsFilter(DBGridConstant::FILTER_TYPE_SELECT, "admins-status", "Status", $group);
//        $dbgrid->addFetchTable("admins", array('id' , 'username' , 'email' , 'created' , 'last_login' , 'status'));
//        $dbgrid->addValueFilter('admins-is_delete', '0');
//        $dbgrid->addFetchColumn("username", "UserName");
//        $dbgrid->addFetchColumn("created", "Created");
//        $dbgrid->addFetchColumn("email", "Email");
//        $dbgrid->addFetchColumn("last_login", "LastLogin");
//        $dbgrid->addFetchColumn("status", "Status");
//        $dbgrid->setModule('/acl/acl');
//        $dbgrid->setItemActions(array('Edit' => 'edituser' , 'Delete' => 'deleteuser'));
//        $dbgrid->setModuleActions(array('Add' => 'adduser'));
//        $this->view->dbGrid = $dbgrid->render();
//    }
//    public function adduserAction ()
//    {
//        $admin_id = HttpUtil::getSessionString('id', 'user');
//        $form = $this->getUserForm(null, $admin_id);
//        $form->setAction('addusersubmit');
//        $this->view->form = $form;
//    }
//    public function addusersubmitAction ()
//    {
//        $admin_id = HttpUtil::getSessionString('id', 'user');
//        $form = $this->getUserForm(null, $admin_id);
//        ;
//        if (! $form->isValid($_POST)) {
//            $this->view->form = $form;
//            return $this->render('editrole');
//        }
//        $insert['username'] = HttpUtil::getPostString('UserName');
//        $insert['email'] = HttpUtil::getPostString('Email');
//        $insert['password'] = HttpUtil::getPostString('Password');
//        $permissions = HttpUtil::getPostValue('Permissions');
//        $aclserv = new AclService();
//        $aclserv->addUser($insert, $permissions);
//        // $aclserv->updatePermission($user, $permissions, $user_id);
//        $this->_redirect('/acl/acl/getuser');
//        exit();
//    }
//    public function edituserAction ()
//    {
//        $user_id = $this->_getParam('id');
//        $admin_id = HttpUtil::getSessionString('id', 'user');
//        $form = $this->getUserForm($user_id, $admin_id);
//        $this->view->form = $form;
//    }
//    public function editusersubmitAction ()
//    {
//        $user_id = $this->_getParam('id');
//        $admin_id = HttpUtil::getSessionString('id', 'user');
//        $form = $this->getUserForm($user_id, $admin_id);
//        ;
//        if (! $form->isValid($_POST)) {
//            $this->view->form = $form;
//            return $this->render('editrole');
//        }
//        $user_id = $this->_getParam('id');
//        $set['username'] = HttpUtil::getPostString('UserName');
//        $set['email'] = HttpUtil::getPostString('Email');
//        $set['password'] = HttpUtil::getPostString('Password');
//        $permissions = HttpUtil::getPostValue('Permissions');
//        $aclserv = new AclService();
//        $user['set'] = $set;
//        $user['where'] = $user_id;
//        $aclserv->updatePermission($user, $permissions, $user_id);
//        $this->_redirect('/acl/acl/getuser');
//        exit();
//    }
//    public function deleteuserAction ()
//    {
//        $user_id = $this->_getParam('id');
//        $aclserv = new AclService();
//        $aclserv->deleteUser($user_id);
//        $this->_redirect('/acl/acl/getuser');
//    }
//    public function getUserForm ($user_id, $admin_id = null)
//    {
//        $aclserv = new AclService();
//        $permissionlist = $aclserv->getPermissionsByUserId($admin_id);
//        if ($user_id) {
//            $percheck = $aclserv->getPermissionsByUserId($user_id);
//            $permissionlist = $aclserv->getPermissionsByUserId($admin_id, $percheck);
//            $user = $aclserv->getUserById($user_id);
//            $form = new UserForm($permissionlist, $user, $percheck);
//            return $form;
//        } else {
//            $form = new UserForm($permissionlist);
//            return $form;
//        }
//    }
//    public function getAclForm ($admin_id = null, $role_id = null)
//    {
//        $aclserv = new AclService();
//        $role_perm = array();
//        $admin_perm = $aclserv->getPermissionsByUserId($admin_id);
//        if ($role_id) {
//            $role_perm = $aclserv->getPermissionByRoleId($role_id);
//            $admin_perm = $aclserv->getPermissionsByUserId($admin_id, $role_perm);
//        }
//        $permission['role'] = $role_perm;
//        $permission['admin'] = $admin_perm;
//        if ($role_id) {
//            $role = $aclserv->getRoleById($role_id);
//            $form = new RoleForm($permission, $role['role_name']);
//            return $form;
//        } else {
//            $form = new RoleForm($permission);
//            return $form;
//        }
//    }

    public function noaccessAction(){
       
    }
}
